﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace _2DGame
{
    class Obstacle
    {

        static Vector2 Position;
        Texture2D Texture;
        int StageWidth, StageHeight;

        static bool collision = false; //Value for respawn. - Mats och Fredrik

        //Constructor - Fredrik & Mats
        public Obstacle(Texture2D texture, Vector2 position, int stageWidth, int stageHeight)
        {
            Texture = texture;
            Position = position;
            StageWidth = stageWidth;
            StageHeight = stageHeight;

        }
        static public bool collisionDetection
        {
            get { return collision; }
            set { collision = value; }
        }

        static public Vector2 positionDetection
        {
            get { return Position; }
            set { Position = value; }
        }

        //Updates speed and position of obstacle - Fredrik & Mats added inparameter for sound johan och christian
        public void MoveObstacle(GameTime gameTime, int speed, Player player, GameBordI gameBoard, SpriteBatch spriteBatch, SoundEffect Crash, GraphicsDeviceManager graphics)
        {
            Position.X -= LevelController.SpeedRunner();

            if (Position.X < StageWidth - (StageWidth + Texture.Width + 10))
            {
                Position.X = StageWidth + 10;
            }

            float CatRightside = player.PlayerCord.X + 73;
            float CatLeftside = player.PlayerCord.X;
            float CatBottom = player.PlayerCord.Y + 53;
            float CatTop = player.PlayerCord.Y;
            float ObstacleLeftside = Position.X + 11;
            float ObstacleRightside = Position.X + (Texture.Width - 13);
            float ObstacleTop = Position.Y + 68;
            float ObstacleBottom = Position.Y + 100;
            float CurrentSpeed = LevelController.SpeedRunner();

            //If the cats right side is equal to the obstacles left side.
            //Need adjustments.? faller igenom uppe i stenens vänstra hörn. Hål?
            if (ObstacleLeftside <= (CatRightside - 1) && ObstacleLeftside >= (CatRightside - CurrentSpeed))
            {
                //If the bottom of the cat is equal to or lower then the top of the obstacle.
                if (CatBottom >= ObstacleTop)
                {
                    //If the top of the cat is equal to or higher then the bottom of the obstacle.
                    if (CatTop <= ObstacleBottom)
                    {
                        player.Life -= 1;//Cats life - 1.
                        gameBoard.UpdateLife(spriteBatch, player.Life);//Call the method to update the amount of lifes on the screen.
                        Crash.Play(1F, 0.5F, 0F);//sound

                     
                        //Value for respawn. - Mats och Fredrik
                        collision = true;

                    }
                }
            }
            //If the cat land on top of the obstacle.
            else if (ObstacleLeftside <= CatRightside && ObstacleRightside >= CatLeftside)
            {
                if (CatBottom >= (ObstacleTop - 2) && CatBottom <= (ObstacleTop + 2))
                {
                    player.StopFalling(graphics, (Position.Y + 68));//Stops the cat from falling.
                }

            }
            //If the cat walks off the stone.
            else if (player.PlayerCord.X >= (Position.X + (Texture.Width - 13)) && player.PlayerCord.X <= ((Position.X + (Texture.Width - 13))) + CurrentSpeed)
            {
                if ((player.PlayerCord.Y + 53) == (Position.Y + 68))
                {
                    player.ResetPlayer(graphics);//Makes the cat fall down to the ground again.
                }
            }
        }

        //Updates - Fredrik & Mats
        public void Update(GameTime gameTime, int speed)
        {
        }

        //Texture properties
        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(Texture, Position, Color.White);
            spriteBatch.End();
        }
    }
}
